<?php
echo '<?xml version="1.0" encoding="UTF-8" />';
echo $this->doctype();

$config = Zend_Registry::get('config');
$userCity = $this->userCity();
	    ?>
	
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<title><?php if($this->pageTitle != null) 
	echo $this->pageTitle;
	else{
	?>HeyIdleBooks (<?php echo $userCity->name;?>) | Rent, Sell, Exchange your idle books here! <?php }?>
	</title>
	<meta name="description" content="<?php if($this->pageDesc != null) {
		echo $this->pageDesc;
	} else{?>HeyIdleBooks (<?php echo $userCity->name; ?>) | Second-Hand Book, rent, exchange, sell, sale, unused books  <?php }?>"/>
	
	<?php 
	$this->headLink()->prependStylesheet($config->baseurl.'/css/layout.css');
	$this->headLink()->prependStylesheet($config->baseurl.'/css/reset.css');

	$this->headScript()->prependFile($config->baseurl.'/js/jquery.fancybox-1.3.4.pack.js');
	$this->headScript()->prependFile($config->baseurl.'/js/jquery.mousewheel-3.0.4.pack.js');
	$this->headScript()->prependFile($config->baseurl.'/js/jquery-1.4.2.min.js');
	
	
	echo $this->headLink();
	?>	
	<link type="image/x-icon"  rel="icon" href="<?php echo $config->baseurl."/img/favicon.ico"?>">
	<link type="image/x-icon"  rel="shortcut icon" href="<?php echo $config->baseurl."/img/favicon.ico"?>">
</head>
<body <?php 
if($this->bodyId !=null)
echo "id='".$this->bodyId."'";
?>>
<div id="baseUrl"><?php echo $config->baseurl4ajax;?></div>
<div id="wrapper">
	<div id="topBar"></div>

	<div id="headerWrapper">
		<a id="logo" href="<?php echo $this->url(array('controller'=>'index','action'=>'index'),"default",true); ?>"></a>
		<div id="headerRight">
			<ul id="headerMenu">
				<li><?php 
	    echo strtoupper($userCity->name);
				?><a class="fancybox" href="<?php echo $this->url(array('controller'=>'user','action'=>'location'),"default",true); ?>">Change</a></li>
			</ul>
			
			<div id="headerSearch">
			    <form method="get" action="<?php echo $this->url(array("controller"=>"book", "action"=>"list"), "default", true)?>">
				<input type="text" id="keywords" name="keywords" title="Type ISBN, Title, Author ..." class="defaultText"></input>
				<input type="submit" id="search"></input>
				</form>
			</div>
			
			<ul id="headerNav">
			<?php
 $userData = $this->loggedInUser();
 if ($userData != null) {?> 
 hi, <?php 
 $user = new User();
 $user = $user->findById($userData->id);
 echo $user->name;
 
 ?>
<a href="<?php echo $this->url(array('controller'=>'index','action'=>'index'),"default",true); ?>">Home</a> 
<a href="<?php echo $this->url(array('controller'=>'user','action'=>'profile'),"default",true); ?>">Profile</a> 
<a href="<?php echo $this->url(array('controller'=>'user','action'=>'booklist'),"default",true); ?>">Books</a>
<a href="<?php echo $this->url(array('controller'=>'message','action'=>'inbox'),"default",true); ?>">Messages <?php 
$cnt = count(User::getUnreadMessageByUser($user));  
if ($cnt>0) {

?><span id="msgNew"><?php echo $cnt;?></span><?php }?></a> 
<a href="<?php echo $this->url(array('controller'=>'user','action'=>'settings'),"default",true); ?>">Settings</a>
<a href="<?php echo $this->url(array('controller'=>'user','action'=>'logout'),"default",true); ?>">Logout</a> 

			<?php }else { ?>
				<li>
<?php 
if (empty($_REQUEST["next"])) {
?>
<a id="login_fb" 
href="<?php echo $this->url(array("controller"=>"user", "action"=>"fblogin"), "default", true)?>">Log in with Facebook</a>
<?php } else {?>
<a id="login_fb" 
href="<?php echo $this->url(array("controller"=>"user", "action"=>"fblogin", "next"=>urlencode($_REQUEST["next"])), "default", true)?>">Log in with Facebook</a>
<?php }?>
<div style="float:right;"><strong>Rent, Sell, Exchange your idle books here!</strong> Please first 
				<a style="margin-left:0;" href="<?php echo $this->url(array('controller'=>'user','action'=>'login'),"default",true); ?>">Log in</a>
				 or <a style="margin-left:0;"  href="<?php echo $this->url(array('controller'=>'user','action'=>'register'),"default",true); ?>">Sign up</a>
				 or</div></li>
				 <?php } ?>
			</ul>
		</div>
	</div>
	
	<div id="contentWrapper">
	<?php if($this->pageName != "home" || $userData != null) {
	// user has logged in and it's not homepage
		?>
	<div class="dashline"></div>
	<div id="contentOuter">
	<div id="content">
<?php
if(count($this->flashmsgs)>0) {?> <div class="flashMsg"> <?php
foreach($this->flashmsgs as $msg) {
	echo $msg;
}?> </div> <?php }?>

<?php echo $this->layout()->content; ?>
	</div>
	
<?php } else {
	// user hasn't logged in or it's homepage
if(count($this->flashmsgs)>0) {?><div class="flashMsg"><?php 
foreach($this->flashmsgs as $msg) {
	echo $msg;
}?> </div> <?php }?>
		
<?php echo $this->layout()->content;
}?>
	</div>

<ul id="footer">
<li style="float:left;margin-left:0;">Copyright © 2011 HeyIdleBooks.com All Rights Reserved</li>
<li><a href="http://heyidlebooks.wordpress.com">Blog</a></li>
<li><a href="<?php echo $this->url(array('controller'=>'index','action'=>'contact'), "contact", true); ?>">Contact Us</a></li>
<li><a href="<?php echo $this->url(array('controller'=>'index','action'=>'terms'), "terms", true); ?>">Terms</a></li>
<li><a href="<?php echo $this->url(array('controller'=>'index','action'=>'about'), "about", true); ?>">About Us</a></li>

</ul>
</div>
	</div>

</body>
<?php echo $this->headScript();?>
<script type="text/javascript">
$(document).ready(function()
{	
	if ($(".fancybox").length>0)
		$(".fancybox").fancybox();
	
	 $("[title]").not(".defaultText").style_my_tooltips({
		 tip_follows_cursor: "on", //on/off
		 tip_delay_time: 1000 //milliseconds
		});  
	
	//input default value
    $(".defaultText").focus(function(){
    	if ($(this).val() == $(this)[0].title){
            $(this).removeClass("defaultTextActive");
            $(this).val("");}});
    
    $(".defaultText").blur(function(){
        if ($(this).val() == ""){
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);}});
    
    $(".defaultText").blur();   
    
	//list
	$(".books li, #mailList li, .activities li, li.rss, #search").live("mouseover",function(){
		$(this).addClass('hover');
		$(this).css("cursor","pointer");
	});
	$(".books li, #mailList li, .activities li, li.rss, #search").live("mouseout",function(){
		$(this).removeClass('hover');
		$(this).css("cursor","default");
	});
	
	$(".books:not(#booklist) li, li.rss").live("click",function(){
		location.href = $(this).find("a").attr("href");});
	

	$("#mailList li,.activities li").live("click",function(){
		location.href = $(this).find("h2 a").attr("href");});
	
	//detail
	$(".short a").click(function(){
		$(".short").hide();
		$(".full").slideDown();});
	$(".full a").click(function(){
		$(".full").hide();
		$(".short").show();});
	
	$(".loading").ajaxStart(function(){
		   $(this).show();
		 }).ajaxStop(function() {
			 $(this).hide();
	});

	  
    if ($("select#city option").length > 1){
    	$("#city-label,#city-element").show();
    }
    if ($("select#region option").length > 1){
    	$("#region-label,#region-element").show();
    }

    $baseUrl=$("#baseUrl").text();  
    
    //live change doesn't work in ie jquery 1.4.2
    $("#settingForm #country, #registerForm #country, #locationLoggedIn #country, #locationUnLoggedIn #country").bind("change",function(){
    	$formId = $(this).parents("form").attr("id");
    	
    	$.ajax({
    		  url: $baseUrl+ '/user/citylistajax',
    		  type: 'post',
    		  dataType: 'json',
    		  async: false,
    		  data: ({countryId : parseInt($(this).val())}),
    		  success: function(data) {
    			if(data.exist) {
    				$("form[id='"+$formId+"']").find("#city").empty().append(data.list);
    				$("form[id='"+$formId+"']").find("#city-label,#city-element").show();
    			} else {
    				$("form[id='"+$formId+"']").find("#city").empty().append(data.list);
    				$("form[id='"+$formId+"']").find("#city-label,#city-element").hide();
    				$("form[id='"+$formId+"']").find("#region-label,#region-element").hide();
    			}
    		  }});   
    });
    
    $("#settingForm #city, #registerForm #city, #locationLoggedIn #city, #locationUnLoggedIn #city").bind("change",function(){
    	
    	$formId = $(this).parents("form").attr("id");
    	
    	$.ajax({
    		  url: $baseUrl+ '/user/regionlistajax',
    		  type: 'post',
    		  dataType: 'json',
    		  async: false,
    		  data: ({cityId : parseInt($(this).val())}),
    		  success: function(data) {
    			if(data.exist) {
    				$("#"+$formId + " #region").empty().append(data.list);
    				$("#"+$formId + " #region-label, #"+$formId + " #region-element").show();
    			//	$("form[id='"+$formId+"']").find("#region").empty().append(data.list);
    			//	$("form[id='"+$formId+"']").find("#region-label,#region-element").show();
    			} else {
    				$("#"+$formId + " #region").empty().append(data.list);
    				$("#"+$formId + " #region-label, #"+$formId + " #region-element").hide();
    				//$("form[id='"+$formId+"']").find("#region").empty().append(data.list);
    				//$("form[id='"+$formId+"']").find("#region-label,#region-element").hide();
    			}
    		  }});   
    });
    
    
    $("#locationLoggedIn, #locationNonLoggedIn").submit(function(e){
    	e.preventDefault();
    	   	
    	if ($("#locationLoggedIn #region-element").length > 0) {
    		//LoggedIn
    		
    		if ($("#locationLoggedIn #region-element").css("display")=="block") {
    			//region is provided
    			$cityId = $('#locationLoggedIn select#region option:selected').val();

    	    	if ($cityId == 0){
    	    		$("#locationLoggedIn #region-element")
    	    		.append('<label for="region" generated="true" class="error">Please select one region ...</label>');
    	    	}
    	    	
    		} else { 
    			//region is not provided
    			$cityId = $('#locationLoggedIn select#city option:selected').val();
    			if ($cityId == 0){
    	    		$("#locationLoggedIn #city-element")
    	    		.append('<label for="city" generated="true" class="error">Please select one city ...</label>');
    	    	} 
    		}
    	} else {
    		//Not LoggedIn, or loggedIn but No region is provided  
		
    		$cityId = $('#locationNonLoggedIn select#city option:selected').val();
	    	if ($cityId == 0){
	    		$("#locationNonLoggedIn #city-element")
	    		.append('<label for="city" generated="true" class="error">Please select one city ...</label>');
	    	} 
    	}
    	
    	if ($cityId != 0){
    		$.ajax({
    			url: $baseUrl+ '/user/locationajax',
    			type: 'post',
    			async: false,
    			data: ({cityId : parseInt($cityId)}),
    			success: function(data) {
    			location.href=location.href;
  		  	}}); 
    	}
    	
    });
    
	
});
</script>